package com.xsglxt;

import java.util.ArrayList;
import java.util.Scanner;

public class Menu {
    private final Scanner scanner = new Scanner(System.in);
    private final Manage manage = new Manage();

    public void start() {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            handleChoice(choice);
        }
    }

    //显示信息
    private void printStudentHeader() {
        System.out.println("-------------------------------------------------------------------");
        System.out.printf("%-10s %-10s %-10s %-15s\n", "姓名", "年龄", "学号", "地址");
        System.out.println("-------------------------------------------------------------------");
    }
    private void printStudentInfo(Student student) {
    System.out.printf("%-10s %-10s %-10s %-15s\n",
            student.getName(),
            student.getAge() + "岁",
            student.getId(),
            student.getAddress());


}


    private void showMenu() {
        System.out.println("-----欢迎使用学生管理系统-----");
        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 修改学生");
        System.out.println("4. 查询学生");
        System.out.println("5. 查询所有学生");
        System.out.println("6. 退出系统");
        System.out.println("请输入你的选择：");
    }
    private void handleChoice(int choice) {
        switch (choice) {
            //添加学生信息
            case 1 -> {
                    System.out.println("请输入学生学号：");
                    String id = scanner.next();
                    //判断学号是否已存在
                    if (manage.findStudent(new Student(null, 0, id, null)) != null)
                        System.out.println("该学号已存在，请重新输入");

                    //学号不存在，添加学生信息
                    else {
                        System.out.println("请输入学生姓名：");
                        String name = scanner.next();
                        System.out.println("请输入学生年龄：");
                        int age = scanner.nextInt();
                        System.out.println("请输入学生地址：");
                        String address = scanner.next();
                        Student student = new Student(name, age, id, address);
                        manage.addStudent(student);

                    }

            }
            ///删除学生信息
            case 2 -> {
                System.out.println("请输入删除学生学号：");
                String sid = scanner.next();
                if (manage.deleteStudent(sid)) {
                    System.out.println("删除学生信息成功");
                } else {
                    System.out.println("该信息不存在，请确认输入是否正确");
                }
            }
            //修改学生信息
            case 3 -> {
                System.out.println("请输入你要修改的学生的学号：");
                String sid = scanner.next();
                ArrayList<Student> students = manage.getStudents();
                int index = Manage.getIndex(students, sid);
                if (index != -1) {
                    Student student = students.get(index);
                    System.out.println("请输入学生新姓名：");
                    String name = scanner.next();
                    System.out.println("请输入学生新年龄：");
                    int age = scanner.nextInt();
                    System.out.println("请输入学生新地址：");
                    String address = scanner.next();

                    student.setName(name);
                    student.setAge(age);
                    student.setAddress(address);
                    System.out.println("修改学生成功");
                }
                else {
                    System.out.println("该学号不存在，请确认输入是否正确");
                }
            }
            //查询指定学生信息
            case 4 -> {
                    System.out.println("请输入要查询的学生学号：");
                    String id = scanner.next();
                    Student student = manage.findStudent(new Student(null, 0, id, null));
                    if (student == null) {
                        System.out.println("该信息不存在，请确认学号是否正确");

                    } else {
                        System.out.println("查询学生成功");
                        printStudentInfo(student); // 调用统一格式化输出方法
                    }

            }
            //查询所有学生信息
            case 5 -> {
                ArrayList<Student> students = manage.findAllStudent();
                if(students.isEmpty()){
                    System.out.println("无信息,请先添加信息再查询");
                }
                else{
                    printStudentHeader();
                    for (Student student : students) {
                        printStudentInfo(student); // 调用统一格式化输出方法;
                    }
                }

            }
            //退出系统
            case 6 -> {
                System.out.println("感谢使用学生管理系统");
                System.exit(0);
            }

        }
    }


}

